/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.fragment.nativeview;

import it.gmariotti.cardslib.demo.ResourceTable;
import it.gmariotti.cardslib.demo.cards.CustomCard;
import it.gmariotti.cardslib.demo.fragment.BaseMaterialFragment;
import it.gmariotti.cardslib.library.Constants;
import it.gmariotti.cardslib.library.internal.Card;
import it.gmariotti.cardslib.library.internal.CardHeader;
import it.gmariotti.cardslib.library.view.CardView;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ScrollView;
import ohos.agp.components.Text;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;

import static ohos.agp.components.Component.HIDE;

/**
 * Card Examples
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class NativeCardFragment extends BaseMaterialFragment {

    protected ScrollView mScrollView;
    protected Text mTextSwipe;

    protected Card mCardCab;
    protected CardView cardViewCab;

    @Override
    protected int getSubTitleHeaderResourceId() {
        return ResourceTable.String_header_title_cardsubtitle;
    }

    @Override
    protected int getTitleHeaderResourceId() {
        return ResourceTable.String_header_title_group1;
    }

    @Override
    protected String getDocUrl() {
        return "";
    }

    @Override
    protected String getSourceUrl() {
        return "";
    }

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_demo_fragment_native_card);
        mScrollView = (ScrollView) findComponentById(ResourceTable.Id_card_scrollview);
        mTextSwipe = (Text) findComponentById(ResourceTable.Id_carddemo_card3_text);
        initCards();
    }


    private void initCards() {
        init_simple_card();
        init_card_inner_layout();
        init_custom_card();
        init_custom_card_swipe();
        init_custom_card_clickable();
        init_custom_card_partial_listener();
        init_cab();
    }

    /**
     * This method builds a simple card
     */
    private void init_simple_card() {

        //Create a Card
        Card card = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        card.addCardHeader(header);

        //Set the card inner text
        try {
            card.setTitle(getResourceManager().getElement(ResourceTable.String_demo_card_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_id);
        cardView.setCard(card);
    }

    /**
     * This method builds a simple card with a custom inner layout
     */
    private void init_card_inner_layout() {

        //Create a Card
        Card card = new Card(this, ResourceTable.Layout_carddemo_example_inner_content);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_header_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        card.addCardHeader(header);

        //Set the card inner text
        try {
            card.setTitle(getResourceManager().getElement(ResourceTable.String_demo_card_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_card_inner);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card
     */
    private void init_custom_card() {

        //Create a Card
        Card card = new CustomCard(this);

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card3);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card with a swipe action
     */
    private void init_custom_card_swipe() {

        //Create a Card
        CustomCard card = new CustomCard(this);

        //Enable a swipe action
        card.setSwipeable(true);

        //You can set a SwipeListener.
        card.setOnSwipeListener(card1 -> {
            if (mTextSwipe != null)
                mTextSwipe.setVisibility(HIDE);
        });

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card3_swipe);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card clickable
     */
    private void init_custom_card_clickable() {

        //Create a Card
        CustomCard card = new CustomCard(this);

        //Set onClick listener
        card.setClickedListener((card1, view) -> new ToastDialog(getContext()).setText("Clickable card").show());

        //Set card in the cardView
        CardView cardView = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card3_clickable);
        cardView.setCard(card);
    }

    /**
     * This method builds a custom card with a swipe action
     */
    private void init_custom_card_partial_listener() {

        //Create a Card
        Card card1 = new Card(this, ResourceTable.Layout_carddemo_example_inner_content);

        CardHeader header1 = new CardHeader(this);
        try {
            header1.setTitle(getResourceManager().getElement(ResourceTable.String_demo_custom_shorttitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        card1.addCardHeader(header1);


        //Set the card inner text
        try {
            card1.setTitle(getResourceManager().getElement(ResourceTable.String_demo_card_shorttitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set a clickListener on ContentArea
        card1.addPartialOnClickListener(Card.CLICK_LISTENER_CONTENT_VIEW, (card, view) -> new ToastDialog(getContext()).setText("Click on Content Area").show());
//
//        //Set card in the cardView
        CardView cardView1 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card3_listeners1);
        cardView1.setCard(card1);

        //----------------------------------------------------------------------------------
        //Create a Card
        Card card2 = new Card(this, ResourceTable.Layout_carddemo_example_inner_content);

        CardHeader header2 = new CardHeader(this);
        try {
            header2.setTitle(getResourceManager().getElement(ResourceTable.String_demo_custom_shorttitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        card2.addCardHeader(header2);


        //Set the card inner text
        try {
            card2.setTitle(getResourceManager().getElement(ResourceTable.String_demo_card_shorttitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        //Set a clickListener on Header Area
        card2.addPartialOnClickListener(Card.CLICK_LISTENER_HEADER_VIEW, (card, view) -> new ToastDialog(getContext()).setText("Click on Header Area").show());


        //Set card in the cardView
        CardView cardView2 = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card3_listeners2);
        cardView2.setCard(card2);


    }

    /**
     * Card with a CAB
     */
    private void init_cab() {

        //Create a Card
        mCardCab = new Card(this);

        //Create a CardHeader
        CardHeader header = new CardHeader(this);

        //Set the header title
        try {
            header.setTitle(getResourceManager().getElement(ResourceTable.String_demo_title_cab1).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }

        mCardCab.addCardHeader(header);

        //Set the card inner text
        try {
            mCardCab.setTitle(getResourceManager().getElement(ResourceTable.String_demo_card_basetitle).getString());
        } catch (Exception e) {
            HiLog.error(Constants.LABEL_LOG, e.toString());
        }


        //Set card in the cardView
        cardViewCab = (CardView) this.findComponentById(ResourceTable.Id_carddemo_example_card_cab);
        cardViewCab.setCard(mCardCab);
    }


}
